package com.learning;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DBHelper extends SQLiteOpenHelper {
	
	Context context;
	

	// Global attributes
	private static final String DATABASE_NAME = "myDatabase.db";
	private static final String DATABASE_TABLE = "contact";
	private static final String DATABASE_CREATE =
	"create table " + DATABASE_TABLE +
	" ( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
	"name TEXT UNIQUE, mail TEXT, phone TEXT);";
	private static final String COLUMN_NAME = "name";
	private static final String COLUMN_MAIL = "mail";
	private static final String COLUMN_PHONE = "phone";	
	private static final String KEY_ID = "_id";
	
	SQLiteDatabase db;
	
	
	public DBHelper(Context context, SQLiteDatabase db) {
		super(context, DATABASE_NAME, null, 1);
		this.context = context;
		this.db = db;
	}
	
	public void insert(String _name, String _mail, String _phone) {
			try {
				
				if(!_name.contentEquals("")) 
				{								
					ContentValues cv = new ContentValues();
					cv.put(COLUMN_NAME, _name);
					cv.put(COLUMN_MAIL, _mail);
					cv.put(COLUMN_PHONE, _phone);
					db.insert(DATABASE_TABLE, null, cv);
				}
			} 
			catch (Exception e) {
				System.out.println("EXCEPTION: " + e.getMessage());
				e.printStackTrace();
			}
		}
	 
	public void create() {
	    	try {
				db.execSQL(DATABASE_CREATE);			
			} 
	    	catch (SQLException e) 
	    	{
				System.out.println("EXCEPTION: " + e.getMessage());
				e.printStackTrace();
			}		
		}
	    
    public void count() {
    	try {
			ArrayList<Contact> list = new ArrayList<Contact>();
			String[] result_columns = new String[] {KEY_ID, COLUMN_NAME, COLUMN_MAIL, COLUMN_PHONE};
			Cursor rows = db.query(true, DATABASE_TABLE, result_columns, null, null, null, null, null, null);
			
			if(rows.moveToFirst()) {
				do {
					int id = rows.getInt(0);
			    	String name = rows.getString(1);
			    	String mail = rows.getString(2);
			    	String phone = rows.getString(3);
			    	Contact ct = new Contact();
			    	ct.setName(name);
			    	ct.setMail(mail);
			    	ct.setPhone(phone);
			    	list.add(ct);
				} while(rows.moveToNext());
			}
			
			
			CharSequence text = "Num of rows: " + list.size();
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(this.context, text, duration);
			toast.show();
		} 
    	catch (Exception e) {
			System.out.println("EXCEPTION: " + e.getMessage());
			e.printStackTrace();
		}		
	}
    
    public void delete(String name) {
    	try {
			if(!name.contentEquals(""))
			{
				db.delete(DATABASE_TABLE, COLUMN_NAME + " = '" + name + "'", null);
				
				Context context = this.context;
				CharSequence text = "Deleted where name = " + name;
				int duration = Toast.LENGTH_SHORT;

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}			
			
		} catch (Exception e) {
			System.out.println("EXCEPTION: " + e.getMessage());
			e.printStackTrace();
		}
    }

	@Override
	public void onCreate(SQLiteDatabase db) {
		create();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			android.util.Log.w("Constants", "Upgrading database, which will destroy all old	data");
			db.execSQL("DROP TABLE IF EXISTS constants");
			onCreate(db);
		}
}
